<?php
/**
 * Created by PhpStorm.
 * User: Happy
 * Date: 2016/5/31 0031
 * Time: 7:44
 */
//手机验证码插件
class mobile_codePlugin extends Plugin{

    /*plugin加载必要的文件**/
    public  function  __construct($path='')
    {
        parent::__construct($path);
        //子组件类
        $plugin_path=$this->config['plugin_path'];
        include($plugin_path.'/lib/yunpian.php'); //加载云片核心发送代码
    }


  public  function generate_code($length = 6) {
        return rand(pow(10,($length-1)), pow(10,$length)-1);
  }


  //展示模板
   public  function  show_demo(){
    //
       $config=$this->config;
       $t=new BlitzPhp();
       $tpl_path=$config['plugin_path'].'/tpl/demo.tpl';
       $plugin_name=basename($config['plugin_path']);
       $css_prefix='./plugins/'.$plugin_name.'/tpl';
       $content=  $t->Binclude($tpl_path,array('css_prefix'=>$css_prefix));
       echo $content; //显示内容
   }
    public  function  send_demo(){
        $phone=I('phone');
        if(!$phone){
            send_data(array('flag'=>false,'msg'=>'请填写手机号码'));
        }
        $config=$this->config;
        //需要开启session做比较用
       $code=$this->generate_code();
        $_SESSION['mobile_code']=$code;

        $yunpian=new yunpian();
        $info= $yunpian->send_code($phone,$code);
        send_data(array('flag'=>true,'msg'=>'验证码已经发送','data'=>$info));
    }
    //
   public  function  compare_code(){
      $code=I('code');
      if(!$code){
         send_data(array('flag'=>false,'msg'=>'请填写收到的验证码'));
      }
       else{
            if($_SESSION['mobile_code']==$code){
                send_data(array('flag'=>true,'msg'=>'验证成功'));
            }
           else{
               send_data(array('flag'=>true,'msg'=>'验证失败'));
           }
       }
   }

   //发送信息 第一个参数为位置信息，第二个参数为内容信息
   public function  send($send_config=array()){

       $config=$this->config;
     /*暂时有如下模板id
      *1431833 支付成功发送给管理员
      *1431829 支付成功发送给顾客
      *1381201 手机验证码注册或者登陆
      */
       $tpl=$send_config['tpl_value'];
       unset($send_config['tpl_value']); //去除键以防止覆盖
       $sending='';
      if($tpl){
          // return '需要设置发送内容';
      foreach($tpl as $key=>$value){
          $sending.='&'.urlencode('#'.$key.'#').'='.urlencode($value);
      }
       $sending=  substr($sending,1);
      }
     $default_config=array(
            'tpl_id'=>'1381201',
            'tpl_value'=>$sending,
            'apikey'=>$config['apikey'],
            'mobile'=>''
           );
         //merge一块
       $config=array_merge($default_config,$send_config);

       $yunpian=new yunpian();

        //发送短信
      $result=  $yunpian->send_msg($config);
        //
       return $result;
   }
   //根据模板id获取短信内容
    //URL：https://sms.yunpian.com/v2/tpl/get.json
   public  function get_tpl_content($tpl_id=''){
       $config=$this->config;
       $yunpian=new yunpian();
       $result= $yunpian->get_tpl_content(array(
           'apikey'=>$config['apikey'],
           'tpl_id'=>$tpl_id,
       ));
       return $result;
   }
   //创建模板 参数分别为签名和内容
    public  function  create_tpl($title='',$content){
        $config=$this->config;
        $yunpian=new yunpian();
        $result= $yunpian->create_tpl(array(
            'apikey'=>$config['apikey'],
            'tpl_content'=>sprintf('【%s】%s',$title,$content),
        ));
        return $result;
    }


}